package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.util.MarketConstant;
import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponExample;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.service.AdminCouponService;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Date;

public class AdminCouponServiceImpl implements AdminCouponService {
    @Override
    public List<MarketCoupon> list(String page, String limit, String name, String type, String status, String sort, String order) {
        SqlSession session = MybatisUtils.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);
        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (StringUtils.isNotEmpty(status)) {
            criteria.andStatusEqualTo(Short.valueOf(status));
        }
        if (StringUtils.isNotEmpty(type)) {
            criteria.andTypeEqualTo(Short.valueOf(type));
        }
        example.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketCoupon> list = mapper.selectByExample(example);
        session.close();
        return list;
    }

    @Override
    public MarketCoupon create(MarketCoupon marketCoupon) {
        SqlSession session = MybatisUtils.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);
        //时间类型为领券相对天数时,必须要设置days>0
        if (marketCoupon.getTimeType() == 0 && marketCoupon.getDays() <= 0) {
            MarketCoupon coupon = new MarketCoupon();
            coupon.setId(-1);
            return coupon;
        }
        //优惠券结束时间早于当前时间


        if ((marketCoupon.getTimeType() == 1 && marketCoupon.getEndTime().isBefore(LocalDate.now().atStartOfDay())) || marketCoupon.getLimit() <= 0) {
            MarketCoupon coupon = new MarketCoupon();
            coupon.setId(-2);
            return coupon;
        }
        //优惠券的每人限领,满减金额不应该小于等于,最低消费不应该小于0
        if ((marketCoupon.getLimit() < 0) || (marketCoupon.getTotal() < 0) || (marketCoupon.getDiscount().signum() != 1) || (marketCoupon.getMin().signum() == -1)) {
            MarketCoupon coupon = new MarketCoupon();
            coupon.setId(-2);
            return coupon;
        }
        //优惠券为指定绝对时间时,要设置正确的时间
        if (marketCoupon.getTimeType() == 1 && (marketCoupon.getStartTime() == null || marketCoupon.getEndTime() == null || marketCoupon.getEndTime().isBefore(marketCoupon.getStartTime()))) {
            MarketCoupon coupon = new MarketCoupon();
            coupon.setId(-3);
            return coupon;
        }
        if (marketCoupon.getTimeType() == 1 && marketCoupon.getStartTime().equals(marketCoupon.getEndTime()) && marketCoupon.getEndTime().equals(LocalDate.now().atStartOfDay())) {
            marketCoupon.setEndTime(marketCoupon.getEndTime().plusDays(1));
        }
        LocalDateTime now = LocalDateTime.now();
        marketCoupon.setAddTime(now);
        marketCoupon.setUpdateTime(now);
        mapper.insertSelective(marketCoupon);
        session.commit();
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        marketCouponExample.createCriteria().andNameEqualTo(marketCoupon.getName());
        MarketCoupon marketCoupon1 = mapper.selectOneByExampleSelective(marketCouponExample);
        session.close();
        return marketCoupon1;

    }

    @Override
    public int delete(MarketCoupon marketCoupon) {
        SqlSession session = MybatisUtils.getSession();
        int effectRowNum = session.getMapper(MarketCouponMapper.class).deleteByPrimaryKey(marketCoupon.getId());
        session.commit();
        session.close();
        return effectRowNum;
    }

    @Override
    public MarketCoupon read(Integer id) {
        SqlSession session = MybatisUtils.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);
        MarketCoupon marketCoupon = mapper.selectByPrimaryKeySelective(id);
        session.commit();
        session.close();
        return marketCoupon;

    }

    @Override
    public MarketCoupon update(MarketCoupon marketCoupon) {
        marketCoupon.setUpdateTime(LocalDateTime.now());
        SqlSession sqlSession = MybatisUtils.getSession();
        MarketCouponMapper mapper = sqlSession.getMapper(MarketCouponMapper.class);
        mapper.updateByPrimaryKeySelective(marketCoupon);
        Integer id = marketCoupon.getId();
        MarketCoupon ret = mapper.selectByPrimaryKey(id);

        sqlSession.commit();
        sqlSession.close();
        return ret;
    }
}
